package com.fushenlan.epidemicControl.service.Impl;

import com.fushenlan.epidemicControl.mapper.NeighborhoodMemberMapper;
import com.fushenlan.epidemicControl.service.NeighborhoodMemberService;
import com.fushenlan.po.NeighborhoodMemberDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @author Administrator
 * @date 2022/5/1 16:11
 */
@Service
@Slf4j
public class NeighborhoodMemberServiceImpl implements NeighborhoodMemberService {

    @Autowired
    private NeighborhoodMemberMapper neighborhoodMemberMapper;

    /**
     * 根据账号和密码查询用户信息
     *
     * @param account 账号
     * @return
     */
    @Override
    public Map<String, Object> findUser(String account) {
        return neighborhoodMemberMapper.findUser(account);
    }

    /**
     * 根据
     *
     * @param id     居委会成员表主键id
     * @param pwd    初始明文密码
     * @param md5Pwd md5加密后的密码
     * @return
     */
    @Override
    public NeighborhoodMemberDO findUserByPwd(Long id, String pwd, String md5Pwd) {
        NeighborhoodMemberDO userById = neighborhoodMemberMapper.findUserById(id);
        if (userById == null) {
            log.info("根据主键id={} 查询用户不存在", id);
            throw new RuntimeException("用户不存在");
        }
        //验证密码是否正确
        String password = userById.getPassword();
        if (!md5Pwd.equals(password)) {
            if (!pwd.equals(password)) {
                log.info("密码MD5后和数据库不一致，继续用初始未加密密码对比，也不一致,数据库密码：{} 请求参数未加密密码：{} 请求参数的密码MD5后：{}", password, pwd, md5Pwd);
                return null;
            }
        }
        return userById;
    }

    /**
     * @param memberDO
     */
    @Override
    @Transactional
    public void update(NeighborhoodMemberDO memberDO) {
        neighborhoodMemberMapper.updateUser(memberDO.getPassword(), memberDO.getId(), memberDO.getUpdateDate());
    }
}
